package com.lbd99.data.lbd.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lbd99.data.lbd.dao.TRechargeMapper;
import com.lbd99.data.lbd.dao.TTransferLogMapper;

@Service
public class TTransferLogService {
	@Autowired
	private TTransferLogMapper tTransferLogMapper;
    @Autowired
    private TRechargeMapper tRechargeMapper;
	
	
	public List<Map<String, Object>> selectListMap(Map<String, Object> map){
		return  tTransferLogMapper.selectListMap(map);
	}


	public List<Map<String, Object>> selectRelationListByMap(Map<String, Object> map) {
		return tTransferLogMapper.selectRelationListByMap(map);
	}


	public List<Map<String, Object>> selectRelationListToday() {
		return tTransferLogMapper.selectRelationListToday();
	}


	public List<Map<String, Object>> selectRelationListWeek(Map<String, Object> map) {
		return tTransferLogMapper.selectRelationListWeek(map);
	};


    /**
     * 如果当前时间在60分钟以内，返回yes(如果未抓取到记录，即返回yes)。如果不在返回no
     * 从t_transfer_log和t_recharge中抓取首次转账时间+60分钟。
     */
	public Boolean compareCurrtimeAndTransfirsttime(Integer userId) {
		Date minDate = null;
		Date tTransferTime = null;
		Date tRechargeTime = null;
		String tTransferTimeStr	= tTransferLogMapper.getMinTTransferTimeByUserid(userId);//首次转账最小时间
		String tRechargeTimeStr	=tRechargeMapper.getMinRechargeTimeByUserid(userId);//首次充值最小时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			if(StringUtils.isNotBlank(tTransferTimeStr)){
				tTransferTime=sdf.parse(tTransferTimeStr);
			}
			if(StringUtils.isNotBlank(tRechargeTimeStr)){
				tRechargeTime=sdf.parse(tRechargeTimeStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(tTransferTime ==null && tRechargeTime== null){//都为空
			return true;
		}
		if(tTransferTime == null &&  tRechargeTime!=null){//一个为空
			minDate=tRechargeTime;
		}
		if(tRechargeTime==null && tTransferTime!=null){//一个为空
			minDate=tTransferTime;
		}
		
		if(tTransferTime!=null && tRechargeTime!=null){//都不为空
			int compareTo = tTransferTime.compareTo(tRechargeTime);
			if(compareTo>=0){
				minDate=tRechargeTime;
			}else{
				minDate=tTransferTime;
			}
		}
		Long diff = minDate.getTime()	+	60 * 60 * 1000 - System.currentTimeMillis();
		if(diff>=0){//符合条件
			return true;
		}else{
			return false;
		}
	}
}
